<template>
  <div id="app">
    <div id="root">
      <div class="todo-container">
        <div class="todo-wrap">
          <MyHeader :addTodo="addTodo"/>
          <MyList :todoList="todoList" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
          <MyFooter
              :changeTodoListAll="changeTodoListAll"
              :deleteAlldoneItem="deleteAlldoneItem"
              :todos="todoList"
          />
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import MyHeader from "./components/MyHeader.vue";
import MyFooter from "./components/MyFooter.vue";
import MyList from "./components/MyList.vue";
export default {
  name: 'App',
  components: {
    MyHeader,
    MyFooter,
    MyList
  },
  data() {
    return {
      todoList: JSON.parse(localStorage.getItem('todoList')) || [] ,
      /*todoList: [
        {
          id: '001',
          title: '吃饭',
          done: false
        },
        {
          id: '002',
          title: '睡觉',
          done: false
        },
        {
          id: '003',
          title: '打豆豆',
          done: true
        }
      ]*/
    }
  },
  watch: {
    todoList: {
      handler(value) {
        localStorage.setItem('todoList',JSON.stringify(value))
      },
      deep:true
    }
  },
  computed: {

  },
  methods: {
    // 添加todo
    addTodo(todoObj) {
      console.log('app组件，我收到了数据：',todoObj)
      this.todoList.unshift(todoObj)
    },

    // 删除todo
    deleteTodo(id) {
      this.todoList = this.todoList.filter(item => item.id !== id)
    },
    // 删除所有已完成
    deleteAlldoneItem() {
      this.todoList = this.todoList.filter(item => !item.done)
    },
    //勾选or取消勾选
    checkTodo(id) {
      this.todoList.forEach(item => {
        if (item.id === id) {
          item.done = !item.done
        }
      })
      console.log(this.isAll)
    },
    // 全选or取消全选
    changeTodoListAll(isChecked) {
      this.todoList.forEach(item => {
        item.done = isChecked
      })
    }
  }
}
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}






</style>
